/*
 * Copyright (C) 2015 andrea
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package meteocal.boundary;

import java.io.Serializable;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.faces.view.ViewScoped;
import javax.inject.Inject;
import javax.inject.Named;
import javax.servlet.http.HttpServletRequest;
import meteocal.control.EventControl;
import meteocal.control.UserControl;
import meteocal.entity.Event;
import meteocal.entity.Users;
import meteocal.entityManager.EventManager;
import meteocal.entityManager.UserManager;
import meteocal.exception.PrivacyException;
import org.primefaces.event.SelectEvent;
import org.primefaces.model.DefaultScheduleModel;
import org.primefaces.model.ScheduleModel;

/**
 *
 * @author andrea
 */
@Named
@ViewScoped
public class OtherUser implements Serializable {
    
    @Inject
    UserManager um;
    
    @Inject
    EventControl ec;
    
    @Inject
    UserControl uc;
    
    @Inject
    EventManager em;
     
    

    private ScheduleModel eventModel;
 
    private MyScheduleEvent event;
    
    private boolean visible;
    
    private Users user;
    
    
  
    @PostConstruct
    public void init() {
        

        HttpServletRequest req = (HttpServletRequest) FacesContext.getCurrentInstance()
        .getExternalContext().getRequest();
        String email=req.getParameter("email");
        if(email==null){
            email=um.getLoggedUser().getEmail();
        }

        
        if(um.findByEmail(email)!= null){
                user = um.findByEmail(email);
        }else{
            user = um.getLoggedUser();
        }
        
            List <Event> calendar;
            
            try {
                
                    calendar = uc.loadCalendar(this.user);
                    event = new MyScheduleEvent();
                    eventModel = new DefaultScheduleModel();
                    visible = true;
                
                    for(Event ev: calendar){
                        Date start = ev.getStartTime().getTime();
                        Date end = ev.getEndTime().getTime();
                        eventModel.addEvent(new MyScheduleEvent(ev.getId(), ev.getCreator().getEmail() ,ev.getTitle(),ev.getPlace(),start,end,ev.isVisible())); //AGGIUNGERE LISTA DEGLI INVITATI PER MOSTRARE INVITATI A EVENTO
                    }
                    
                } catch (PrivacyException ex) {
                
                    visible = false;
                }
                
        
        
            
        }
     
     
    public Date getInitialDate() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(calendar.get(Calendar.YEAR), Calendar.FEBRUARY, calendar.get(Calendar.DATE), 0, 0, 0);
         
        return calendar.getTime();
    } 
     
    public ScheduleModel getEventModel() {
        return eventModel;
    }
     
    public MyScheduleEvent getEvent() {
        return event;
    }
     
    public void onEventSelect(SelectEvent selectEvent) {
        
        event = (MyScheduleEvent) selectEvent.getObject();
        
    }

    /**
     * @return the visible
     */
    public boolean isVisible() {
        return visible;
    }

    /**
     * @param visible the visible to set
     */
    public void setVisible(boolean visible) {
        this.visible = visible;
    }

    /**
     * @return the user
     */
    public Users getUser() {
        return user;
    }

    /**
     * @param user the user to set
     */
    public void setUser(Users user) {
        this.user = user;
    }
    
    private void addMessage(FacesMessage message) {
        FacesContext.getCurrentInstance().addMessage(null, message);
    }
    
    public boolean isFavorite(){
        return um.isFavorite(this.user);
    }
    
    public void addToFavorite(){
        um.addFavorite(this.user);
    }
    
    public void removeFromFavorite(){
        um.removeFavorite(this.user);
    }
    
    public boolean sameUser(){
        
        if(this.user.equals(um.getLoggedUser())){
            return true;
        }
            return false;
    }
}
